package com.weishi.project.training.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.enums.TrainingProgramEnum;
import com.weishi.common.utils.StringUtils;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * TrainingAchievementController
 */
@Api(value = "训练成绩",description = "训练成绩")
@RestController
@RequestMapping("/trainingAchievement/trainingAchievement")
public class TrainingAchievementController extends BaseController {

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     * 测功仪数据 接口
     */
    @Autowired
    private IDynamometerService dynamometerService;

    /**
     * 赛后数据表接口
     */
    @Autowired
    private IRaceDataService raceDataService;

    /**
     * 分段成绩表 接口
     */
    @Autowired
    private IDynamometerSubsectionService dynamometerSubsectionService;

    /**
     * 运动员 接口
     */
    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 船速仪 接口
     */
    @Autowired
    private IShipSpeedService shipSpeedService;




    /**
     * 查询训练成绩列表
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingAchievement trainingAchievement) {
        if(trainingAchievement.getRowingKilometersTwo() == null){
            trainingAchievement.setRowingKilometers(0);
        }else{
            trainingAchievement.setRowingKilometers(trainingAchievement.getRowingKilometersTwo());
        }
        startPage();
        List<TrainingAchievement> list = trainingAchievementService.selectTrainingAchievementList(trainingAchievement);

        // 判断
        if(list.size() > 0){
            list.forEach( p-> {
                // 陆上成绩
                if(p.getFirstLevel().equals("land_specific_training_dynamometer")){
                    List<Dynamometer> dynamometerList = dynamometerService
                            .list(Wrappers.<Dynamometer>lambdaQuery()
                                    .eq(Dynamometer::getSportsmanId , p.getSportsmanId())
                                    .eq(Dynamometer::getGroupId , p.getGroupId())
                                    .eq(Dynamometer::getTrainingDayId , p.getTrainingDayId())
                                    .eq(Dynamometer::getTrainingMode , p.getTrainingMode())
                                    .orderByDesc(Dynamometer::getHeartRate));

                    if(dynamometerList.size() > 0){
                        p.setMaxHeartRate(dynamometerList.get(0).getHeartRate());
                    }

                    // 查询其它心率
                    List<RaceData> raceDataList = raceDataService
                            .list(Wrappers.<RaceData>lambdaQuery()
                                    .eq(RaceData::getSportsmanId , p.getSportsmanId())
                                    .eq(RaceData::getTrainingDayId , p.getTrainingDayId()));

                    // 其它心率
                    String otherHeartRate = "";
                    // 其它乳酸
                    String otherLacticAcid = "";
                    if(raceDataList.size() > 0){
                        for (int i = 0; i<raceDataList.size(); i++){
                            // 判断其它心率
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getHeartRate())){
                                otherHeartRate += raceDataList.get(i).getHeartRateTitle()+":"+raceDataList.get(i).getHeartRate() + " ";
                            }
                            // 判断其它乳酸
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getLacticAcid())){
                                otherLacticAcid += raceDataList.get(i).getLacticAcidTitle()+":"+raceDataList.get(i).getLacticAcid() + " ";
                            }
                        }
                    }
                    // 存入其它心率
                    if(otherHeartRate.length() > 0){
                        String otherHeartRates = otherHeartRate.substring(0 , otherHeartRate.length()-1);
                        p.setOtherHeartRate(otherHeartRates);
                    }
                    // 存入其它乳酸
                    if(otherLacticAcid.length() > 0){
                        String otherLacticAcids = otherLacticAcid.substring(0 , otherLacticAcid.length()-1);
                        p.setOtherLacticAcid(otherLacticAcids);
                    }

                        // 查询分段成绩
                        List<DynamometerSubsection> dynamometerSubsectionList = dynamometerSubsectionService
                                .list(Wrappers.<DynamometerSubsection>lambdaQuery()
                                        .eq(DynamometerSubsection::getSportsmanId , p.getSportsmanId())
                                        .eq(DynamometerSubsection::getGroupId , p.getGroupId())
                                        .eq(DynamometerSubsection::getTrainingDayId , p.getTrainingDayId()));

                        // 分段成绩
                        String averageAchievement = "";
                        // 速度
                        double averageSpeed = 0;
                        // 取最大心率 计算平均心率
                        Integer averageHeartRate = 0;
                        // 平均功率
                        Integer averagePower = 0;
                        // 平均浆频
                        Integer averagePropellerFrequency = 0;
                        // 分段心率
                        String segmentHeartRate = "";
                        // 分段浆频
                        String segmentPropellerFrequency = "";
                        // 分段功率
                        String segmentPower = "";
                        // 分段瓦特
                        String segmentWatt = "";
                        if(dynamometerSubsectionList.size() > 0){
                            for(int i = 0; i<dynamometerSubsectionList.size() ;i++){
                                if(dynamometerSubsectionList.get(i).getTrainingMode() == 0){
                                    averageAchievement += dynamometerSubsectionList.get(i).getLengthTime() + " ";

                                }else{
                                    averageAchievement += dynamometerSubsectionList.get(i).getMileage() + " ";
                                }
                                averageSpeed += dynamometerSubsectionList.get(i).getSpeed();
                                if(StringUtils.isNotNull(dynamometerSubsectionList.get(i).getHeartRate())){
                                    averageHeartRate+=dynamometerSubsectionList.get(i).getHeartRate();
                                }
                                // 存入浆数
                                p.setPulpNumber(dynamometerSubsectionList.get(i).getPulpNumber());
                                p.setPower(dynamometerSubsectionList.get(0).getPower());
                                // 计算平均功率
                                averagePower += StringUtils.turnSecond(dynamometerSubsectionList.get(i).getPower());
                                // 存入浆频
                                p.setPropellerFrequency(dynamometerSubsectionList.get(i).getPropellerFrequency());
                                // 计算平均浆频
                                averagePropellerFrequency += Integer.parseInt(dynamometerSubsectionList.get(i).getPropellerFrequency());
                                // 峰力
                                p.setPeakForce(dynamometerSubsectionList.get(i).getPeakForce());
                                // 均力
                                p.setAverageForce(dynamometerSubsectionList.get(i).getAverageForce());
                                // 平均瓦特
                                p.setAverageWatt(Integer.parseInt(dynamometerSubsectionList.get(0).getWatt()));
                                // 分段心率
                                segmentHeartRate += dynamometerSubsectionList.get(i).getHeartRate() + " ";
                                // 分段浆频
                                segmentPropellerFrequency += dynamometerSubsectionList.get(i).getPropellerFrequency() + " ";
                                // 分段功率
                                segmentPower += dynamometerSubsectionList.get(i).getPower() + " ";
                                // 分段瓦特
                                segmentWatt += dynamometerSubsectionList.get(i).getWatt() + " ";
                            }
                            // 存储分段成绩
                            String segmentedTiming = averageAchievement.substring(0 , averageAchievement.length()-1);
                            p.setSegmentedTiming(segmentedTiming);
                            Double averageSpeed2 = averageSpeed/dynamometerSubsectionList.size();
                            Long a =Math.round(averageSpeed2);
                            Double c = a.doubleValue();
                            p.setAverageSpeed(c.toString());
                            // 存储平均心率
                            p.setAverageHeartRate(averageHeartRate / dynamometerSubsectionList.size());
                            // 存储平均功率
                            Integer averagePowers = averagePower/dynamometerSubsectionList.size();
                            p.setAveragePower(StringUtils.millisecondRotationTime(averagePowers));
                            // 平均浆频
                            p.setAveragePropellerFrequency(averagePropellerFrequency/dynamometerSubsectionList.size());
                            // 存储分段心率
                            p.setSegmentHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                            // 存储分段浆频
                            p.setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0 , segmentPropellerFrequency.length()-1));
                            // 存储分段功率
                            p.setSegmentPower(segmentPower.substring(0 , segmentPower.length()-1));
                            // 存储分段瓦特
                            p.setSegmentWatt(segmentWatt.substring(0 , segmentWatt.length()-1));
                        }


                    // 水上成绩
                }else if(p.getFirstLevel().equals("special_training_water")){

                    // 查询其它心率
                    List<RaceData> raceDataList = raceDataService
                            .list(Wrappers.<RaceData>lambdaQuery()
                                    .eq(RaceData::getSportsmanId , p.getSportsmanId())
                                    .eq(RaceData::getTrainingDayId , p.getTrainingDayId()));

                    // 其它心率
                    String otherHeartRate = "";
                    // 其它乳酸
                    String otherLacticAcid = "";
                    if(raceDataList.size() > 0){
                        for (int i = 0; i<raceDataList.size(); i++){
                            // 判断其它心率
                            System.out.println();
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getHeartRate())){
                                otherHeartRate += raceDataList.get(i).getHeartRateTitle()+":"+raceDataList.get(i).getHeartRate() + " ";
                            }
                            // 判断其它乳酸
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getLacticAcid())){
                                otherLacticAcid += raceDataList.get(i).getLacticAcidTitle()+":"+raceDataList.get(i).getLacticAcid() + " ";
                            }
                        }
                    }
                    // 存入其它心率
                    if(otherHeartRate.length() > 0){
                        String otherHeartRates = otherHeartRate.substring(0 , otherHeartRate.length()-1);
                        p.setOtherHeartRate(otherHeartRates);
                    }
                    // 存入其它乳酸
                    if(otherLacticAcid.length() > 0){
                        String otherLacticAcids = otherLacticAcid.substring(0 , otherLacticAcid.length()-1);
                        p.setOtherLacticAcid(otherLacticAcids);
                    }


                    /**
                     * 把之前的字段重新赋值
                     */
                    // 水上重新赋值分段浆频
                    p.setSegmentPropellerFrequency(p.getPropellerFrequency());
                    // 计算平均浆频
                    if(!"".equals(p.getPropellerFrequency()) && StringUtils.isNotNull(p.getPropellerFrequency())){
                        List<String> averagePropellerFrequency = StringUtils.splitsSpace(p.getPropellerFrequency());
                        Integer averagePropellerFrequency2 = 0;
                        if(averagePropellerFrequency.size() > 0){
                            for (int j = 0; j<averagePropellerFrequency.size(); j++){
                                averagePropellerFrequency2 += Integer.parseInt(averagePropellerFrequency.get(j));
                            }
                            p.setAveragePropellerFrequency(averagePropellerFrequency2/averagePropellerFrequency.size());
                        }
                    }

                    // 重新赋值 心率
                    if(!"".equals(p.getHeartRate()) && StringUtils.isNotNull(p.getHeartRate())){
                        String segmentHeartRate = p.getHeartRate();
                        p.setSegmentHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                    }

                }
            });
        }

        return getDataTable(list);
    }

    /**
     * 导出训练成绩列表
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:export')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TrainingAchievement trainingAchievement) {
        List<AchievementExcel> list = trainingAchievementService.selectTrainingAchievementListExcel(trainingAchievement);
        AjaxResult ajaxResult;

        // 设置存储运动员 id 的容器
        List<Integer> userId = new ArrayList<>();
        for (int i = 0; i<list.size(); i++) {

            // 设置水上陆上中文名称
            if (list.get(i).getType().equals(TrainingProgramEnum.LANDSPECIFICTRAININGDYNAMOMETER.getKey())) {
                // 陆上训练
                list.get(i).setFirstLevelName(TrainingProgramEnum.LANDSPECIFICTRAININGDYNAMOMETER.getName());

                List<TrainingAchievement> trainingAchievementList = trainingAchievementService
                        .list(Wrappers.<TrainingAchievement>lambdaQuery()
                                .eq(TrainingAchievement::getChannelId, list.get(i).getChannelId()));
                if (trainingAchievementList.size() > 0) {
                    trainingAchievementList.forEach(pp -> {
                        userId.add(pp.getSportsmanId());
                    });
                }

                // 根据 运动员的 id 查询运动员的名称 并且放到返回值中
                if (userId.size() > 0) {
                    for (int j = 0; j < userId.size(); j++) {
                        List<Sportsman> sportsmanList = sportsmanService
                                .list(Wrappers.<Sportsman>lambdaQuery()
                                        .eq(Sportsman::getId, userId.get(j)));

                        if (sportsmanList.size() > 0) {
                            // 运动员名称
                            String sportsmanName = "";
                            for (int g = 0; g < sportsmanList.size(); g++) {
                                sportsmanName += sportsmanList.get(g).getName() + " ";
                            }
                            list.get(i).setSportsmanName(sportsmanName);
                        }
                    }
                }


                // 查询分段成绩数据
                // 查询分段成绩
                List<DynamometerSubsection> dynamometerSubsectionList = dynamometerSubsectionService
                        .list(Wrappers.<DynamometerSubsection>lambdaQuery()
                                .eq(DynamometerSubsection::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(DynamometerSubsection::getGroupId, list.get(i).getGroupId())
                                .eq(DynamometerSubsection::getTrainingDayId, list.get(i).getTrainingDayId()));

                // 平均功率
                Integer averagePower = 0;
                // 平均浆频
                Integer averagePropellerFrequency = 0;
                // 分段成绩
                String averageAchievement = "";
                // 分段成绩下标
                Integer averageAchievementIndex = 0;
                // 分段功率
                String segmentPower = "";
                // 分段功率下标
                Integer segmentPowerIndex = 0;
                // 速度
                double averageSpeed = 0;
                // 分段浆频
                String segmentPropellerFrequency = "";
                // 分段浆频下标
                Integer segmentPropellerFrequencyIndex = 0;
                // 分段心率
                String segmentHeartRate = "";
                // 分段心率下标
                Integer segmentHeartRateIndex = 0;
                // 分段瓦特
                String segmentWatt = "";
                // 分段瓦特下标
                Integer segmentWattIndex = 0;
                // 平均心率
                Integer averageHeartRate = 0;

                for (int j = 0; j < dynamometerSubsectionList.size(); j++) {
                    averagePower += StringUtils.turnSecond(dynamometerSubsectionList.get(j).getPower());
                    // 存储平均功率
                    Integer averagePowers = averagePower / dynamometerSubsectionList.size();
                    list.get(i).setAveragePower(StringUtils.millisecondRotationTime(averagePowers));
                    // 计算平均浆频
                    averagePropellerFrequency += Integer.parseInt(dynamometerSubsectionList.get(j).getPropellerFrequency());
                    list.get(i).setAveragePropellerFrequency(averagePropellerFrequency);
                    // 平均瓦特
                    list.get(i).setAverageWatt(Integer.parseInt(dynamometerSubsectionList.get(0).getWatt()));
                    if (dynamometerSubsectionList.get(j).getTrainingMode() == 0) {
                        averageAchievementIndex++;
                        averageAchievement += "分段成绩"+averageAchievementIndex+": "+dynamometerSubsectionList.get(j).getLengthTime() + " " + String.valueOf((char)10);

                    } else {
                        averageAchievementIndex++;
                        averageAchievement += "分段成绩"+averageAchievementIndex+": "+dynamometerSubsectionList.get(j).getMileage()+"米" + " " + String.valueOf((char)10);
                    }
                    // 存储分段成绩
                    String segmentedTiming = averageAchievement.substring(0, averageAchievement.length() - 1);
                    list.get(i).setSegmentedTiming(segmentedTiming);
                    // 存入浆数
                    list.get(i).setPulpNumber(dynamometerSubsectionList.get(j).getPulpNumber());
                    // 分段功率
                    segmentPowerIndex++;
                    segmentPower += "分段功率"+segmentPowerIndex+": "+dynamometerSubsectionList.get(j).getPower() + " " + String.valueOf((char)10);
                    // 存入分段功率
                    list.get(i).setSegmentPower(segmentPower.substring(0, segmentPower.length() - 1));
                    // 存入峰力
                    list.get(i).setPeakForce(dynamometerSubsectionList.get(j).getPeakForce());
                    // 存入均力
                    list.get(i).setAverageForce(dynamometerSubsectionList.get(j).getAverageForce());
                    // 存入速度
                    averageSpeed += dynamometerSubsectionList.get(j).getSpeed();
                    Double averageSpeed2 = averageSpeed / dynamometerSubsectionList.size();
                    Long a = Math.round(averageSpeed2);
                    Double c = a.doubleValue();
                    list.get(i).setAverageSpeed(c.toString());
                    // 分段浆频
                    segmentPropellerFrequencyIndex++;
                    segmentPropellerFrequency += "分段浆频"+segmentPropellerFrequencyIndex+": "+dynamometerSubsectionList.get(j).getPropellerFrequency() + " " + String.valueOf((char)10);
                    // 存储分段浆频
                    list.get(i).setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0, segmentPropellerFrequency.length() - 1));
                    // 分段心率
                    segmentHeartRateIndex++;
                    segmentHeartRate += "分段心率"+segmentHeartRateIndex+": "+dynamometerSubsectionList.get(j).getHeartRate() + " " + String.valueOf((char)10);
                    // 存储分段心率
                    list.get(i).setSegmentHeartRate(segmentHeartRate.substring(0, segmentHeartRate.length() - 1));
                    // 分段瓦特
                    segmentWattIndex++;
                    segmentWatt += "分段瓦特"+segmentWattIndex+": "+dynamometerSubsectionList.get(j).getWatt() + " " + String.valueOf((char)10);
                    // 存储分段瓦特
                    list.get(i).setSegmentWatt(segmentWatt.substring(0, segmentWatt.length() - 1));
                    // 计算平均心率
                    if (StringUtils.isNotNull(dynamometerSubsectionList.get(j).getHeartRate())) {
                        averageHeartRate += dynamometerSubsectionList.get(j).getHeartRate();
                    }
                    // 存储平均心率
                    list.get(i).setAverageHeartRate(averageHeartRate / dynamometerSubsectionList.size());


                }


                /**
                 * 查询最大心率
                 */
                List<Dynamometer> dynamometerList = dynamometerService
                        .list(Wrappers.<Dynamometer>lambdaQuery()
                                .eq(Dynamometer::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(Dynamometer::getGroupId, list.get(i).getGroupId())
                                .eq(Dynamometer::getTrainingDayId, list.get(i).getTrainingDayId())
                                .eq(Dynamometer::getTrainingMode, list.get(i).getTrainingMode())
                                .orderByDesc(Dynamometer::getHeartRate));
                if (dynamometerList.size() > 0) {
                    list.get(i).setMaxHeartRate(dynamometerList.get(0).getHeartRate());
                }


                // 查询其它心率
                List<RaceData> raceDataList = raceDataService
                        .list(Wrappers.<RaceData>lambdaQuery()
                                .eq(RaceData::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(RaceData::getTrainingDayId, list.get(i).getTrainingDayId()));

                // 其它心率
                String otherHeartRate = "";
                // 其它乳酸
                String otherLacticAcid = "";
                if (raceDataList.size() > 0) {
                    for (int g = 0; g < raceDataList.size(); g++) {
                        // 判断其它心率
                        if (StringUtils.isNotEmpty(raceDataList.get(g).getHeartRate())) {
                            System.out.println();
                            otherHeartRate += raceDataList.get(g).getHeartRateTitle() + ":" + raceDataList.get(g).getHeartRate() + " " + String.valueOf((char)10);
                        }
                        // 判断其它乳酸
                        if (StringUtils.isNotEmpty(raceDataList.get(g).getLacticAcid())) {
                            System.out.println();
                            otherLacticAcid += raceDataList.get(g).getLacticAcidTitle() + ":" + raceDataList.get(g).getLacticAcid() + " " + String.valueOf((char)10);
                        }
                    }
                }
                // 存入其它心率
                if (otherHeartRate.length() > 0) {
                    String otherHeartRates = otherHeartRate.substring(0, otherHeartRate.length() - 1);
                    list.get(i).setOtherHeartRate(otherHeartRates);
                }
                // 存入其它乳酸
                if (otherLacticAcid.length() > 0) {
                    String otherLacticAcids = otherLacticAcid.substring(0, otherLacticAcid.length() - 1);
                    list.get(i).setOtherLacticAcid(otherLacticAcids);
                }

            } else {
                // 水上训练
                list.get(i).setFirstLevelName(TrainingProgramEnum.WATERPROJECT.getName());
                // 设置航道名称
                list.get(i).setChannelName("第" + Integer.parseInt(list.get(i).getChannelName() + 1) + "航道");
                // 设置船速仪id
                // 根据船速仪 id 查询船速仪信息 ， 并且赋值
                if (!"".equals(list.get(i).getShipSpeedId()) && StringUtils.isNotNull(list.get(i).getShipSpeedId())) {
                    list.get(i).setShipSpeedId(shipSpeedService
                            .getOne(Wrappers.<ShipSpeed>lambdaQuery()
                                    .eq(ShipSpeed::getShipSpeedId, list.get(i).getShipSpeedId()))
                            .getShipSpeedId());
                }

                // 设置人的名称
                List<TrainingAchievement> trainingAchievementList = trainingAchievementService
                        .list(Wrappers.<TrainingAchievement>lambdaQuery()
                                .eq(TrainingAchievement::getChannelId, list.get(i).getChannelId()));
                if (trainingAchievementList.size() > 0) {
                    trainingAchievementList.forEach(pp -> {
                        userId.add(pp.getSportsmanId());
                    });
                }

                // 根据 运动员的 id 查询运动员的名称 并且放到返回值中
                if (userId.size() > 0) {
                    for (int j = 0; j < userId.size(); j++) {
                        System.out.println();
                        List<Sportsman> sportsmanList = sportsmanService
                                .list(Wrappers.<Sportsman>lambdaQuery()
                                        .eq(Sportsman::getId, userId.get(j)));

                        if (sportsmanList.size() > 0) {
                            // 运动员名称
                            String sportsmanName = "";
                            for (int g = 0; g < sportsmanList.size(); g++) {
                                System.out.println();
                                sportsmanName += sportsmanList.get(g).getName() + " ";
                            }
                            list.get(i).setSportsmanName(sportsmanName);
                        }
                    }
                }

                // 设置水上分段成绩
                if(!"".equals(list.get(i).getSegmentedTiming()) && StringUtils.isNotNull(list.get(i).getSegmentedTiming())){
                    List<String> segmentedTimingList = StringUtils.splitsSpace(list.get(i).getSegmentedTiming());
                    String segmentedTiming = "";    // 分段成绩
                    Integer segmentedTimingIndex = 0;   // 分段成绩下标
                    for (int s = 0; s<segmentedTimingList.size(); s++){
                        segmentedTimingIndex++;
                        segmentedTiming += "分段成绩" + segmentedTimingIndex+": " + segmentedTimingList.get(s) + " " + String.valueOf((char)10);
                    }
                    list.get(i).setSegmentedTiming(segmentedTiming);
                }


                // 存储平均浆频
                list.get(i).setAveragePropellerFrequency(list.get(i).getAveragePropellerFrequency());
                // 存储平均船速
                list.get(i).setAverageSpeed(list.get(i).getAverageSpeed());
                // 判断左桨，还是又桨
                if(list.get(i).getOarType() == 0){
                    list.get(i).setOarTypeName("双桨");
                }else if(list.get(i).getOarType() == 1){
                    list.get(i).setOarTypeName("左桨");
                }else if(list.get(i).getOarType() == 2){
                    list.get(i).setOarTypeName("右桨");
                }

                // 计算平均心率
                Integer averageHeartRate = 0;
                if(!"".equals(list.get(i).getHeartRate()) && StringUtils.isNotNull(list.get(i).getHeartRate())){
                    List<String> averageHeartRateList = StringUtils.splitsSpace(list.get(i).getHeartRate());
                    for (int a = 0; a<averageHeartRateList.size(); a++){
                        averageHeartRate += Integer.parseInt(averageHeartRateList.get(a));
                    }
                    // 存入 平均心率
                    list.get(i).setAverageHeartRate(averageHeartRate/averageHeartRateList.size());
                }

                // 查询其它心率
                List<RaceData> raceDataList = raceDataService
                        .list(Wrappers.<RaceData>lambdaQuery()
                                .eq(RaceData::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(RaceData::getTrainingDayId, list.get(i).getTrainingDayId()));

                // 其它心率
                String otherHeartRate = "";
                // 其它乳酸
                String otherLacticAcid = "";
                if (raceDataList.size() > 0) {
                    for (int g = 0; g < raceDataList.size(); g++) {
                        // 判断其它心率
                        System.out.println();
                        if (StringUtils.isNotEmpty(raceDataList.get(g).getHeartRate())) {
                            System.out.println();
                            otherHeartRate += raceDataList.get(g).getHeartRateTitle() + ":" + raceDataList.get(g).getHeartRate() + " " + String.valueOf((char)10);
                        }
                        // 判断其它乳酸
                        System.out.println();
                        if (StringUtils.isNotEmpty(raceDataList.get(g).getLacticAcid())) {
                            System.out.println();
                            otherLacticAcid += raceDataList.get(g).getLacticAcidTitle() + ":" + raceDataList.get(g).getLacticAcid() + " " + String.valueOf((char)10);
                        }
                    }
                }
                // 存入其它心率
                if (otherHeartRate.length() > 0) {
                    String otherHeartRates = otherHeartRate.substring(0, otherHeartRate.length() - 1);
                    list.get(i).setOtherHeartRate(otherHeartRates);
                }
                // 存入其它乳酸
                if (otherLacticAcid.length() > 0) {
                    String otherLacticAcids = otherLacticAcid.substring(0, otherLacticAcid.length() - 1);
                    list.get(i).setOtherLacticAcid(otherLacticAcids);
                }

                // 添加小组
                Integer batch = list.get(i).getBatch()+1;
                list.get(i).setBatchName("小组" + batch);


                // 设置水上分段船速
                if(!"".equals(list.get(i).getSectionalSpeed()) && StringUtils.isNotNull(list.get(i).getSectionalSpeed())){
                    List<String> sectionalSpeedList = StringUtils.splitsSpace(list.get(i).getSectionalSpeed());
                    String sectionalSpeed = "";
                    Integer sectionalSpeedIndex = 0;   // 分段船速下标
                    for (int s = 0; s<sectionalSpeedList.size(); s++){
                        sectionalSpeedIndex++;
                        sectionalSpeed += "分段成绩" + sectionalSpeedIndex+": " + sectionalSpeedList.get(s) + " " + String.valueOf((char)10);
                    }
                    list.get(i).setSectionalSpeed(sectionalSpeed);  // 设置分段船速
                }

                // 设置水上分段浆频
                if(!"".equals(list.get(i).getPropellerFrequency()) && StringUtils.isNotNull(list.get(i).getPropellerFrequency())){
                    List<String> propellerFrequencyList = StringUtils.splitsSpace(list.get(i).getPropellerFrequency());
                    String propellerFrequency = "";
                    Integer propellerFrequencyIndex = 0;   // 分段浆频下标
                    for (int s = 0; s<propellerFrequencyList.size(); s++){
                        propellerFrequencyIndex++;
                        propellerFrequency += "分段浆频" + propellerFrequencyIndex+": " + propellerFrequencyList.get(s) + " " + String.valueOf((char)10);
                    }
                    list.get(i).setPropellerFrequency(propellerFrequency);  // 设置分段浆频
                }

                // 设置水上分段心率
                if(!"".equals(list.get(i).getHeartRate()) && StringUtils.isNotNull(list.get(i).getHeartRate())){
                    List<String> heartRateList = StringUtils.splitsSpace(list.get(i).getHeartRate());
                    String heartRate = "";
                    Integer heartRateIndex = 0;   // 分段心率下标
                    for (int s = 0; s<heartRateList.size(); s++){
                        heartRateIndex++;
                        heartRate += "分段心率" + heartRateIndex+": " + heartRateList.get(s) + " " + String.valueOf((char)10);
                    }
                    list.get(i).setHeartRate(heartRate);  // 设置分段心率
                }




            }
        }


        if(trainingAchievement.getFirstLevel().equals("special_training_water")){
            List<AchievementExcel.AchievementWater> achievementWaters = new ArrayList<>();
            for(AchievementExcel achievementExcel : list){
                achievementWaters.add(achievementExcel.getAchievementWater(achievementExcel));
            }
            ExcelUtil<AchievementExcel.AchievementWater> util = new ExcelUtil<>(AchievementExcel.AchievementWater.class);
            ajaxResult = util.exportExcel(achievementWaters, "皮划艇训练成绩");
        }else{
            List<AchievementExcel.AchievementLand> achievementLands = new ArrayList<>();
            for(AchievementExcel achievementExcel : list){
                achievementLands.add(achievementExcel.getAchievementLand(achievementExcel));
            }
            ExcelUtil<AchievementExcel.AchievementLand> util = new ExcelUtil<>(AchievementExcel.AchievementLand.class);
            ajaxResult = util.exportExcel(achievementLands, "皮划艇测功仪训练成绩");
        }
        return ajaxResult;
    }

    /**
     * 获取训练成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingAchievementService.getById(id));
    }

    /**
     * 新增训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:add')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievement.setCtime(new Date());
        trainingAchievementService.save(trainingAchievement);
        return AjaxResult.success();
    }

    /**
     * 修改训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:edit')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievementService.updateById(trainingAchievement);
        return AjaxResult.success();
    }

    /**
     * 删除训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:remove')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        trainingAchievementService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    
}